package com.kb.design.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kb.design.dto.DesignCommunicateList;
import com.kb.design.entity.DesignComment;
import com.kb.design.entity.DesignCommunicate;
import com.kb.design.mapper.postgreSQL.DesignCommentMapper;
import com.kb.design.mapper.postgreSQL.DesignCommunicateMapper;
import com.kb.design.service.DesignCommunicateService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * (CustomUser)表服务接口
 *
 * @author makejava
 * @since 2023-08-22 10:57:12
 */
@Service
@DS("slave-1")
public class DesignCommunicateServiceImpl extends ServiceImpl<DesignCommunicateMapper, DesignCommunicate> implements DesignCommunicateService {

    @Resource
    private DesignCommunicateMapper designCommunicateMapper;

    @Override
    public DesignCommunicateList searchCommunicateInfo(DesignCommunicateList designCommunicateList) {
        LambdaQueryWrapper<DesignCommunicate> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DesignCommunicate::getType,designCommunicateList.getType()).eq(DesignCommunicate::getProjectId,designCommunicateList.getProjectId());
        List<DesignCommunicate> designCommunicates = designCommunicateMapper.selectList(queryWrapper);
        DesignCommunicateList communicateList = new DesignCommunicateList();

        //为前端低代码展示造空行数据
        ArrayList<DesignCommunicate> arrayList = new ArrayList<>();
        DesignCommunicate designCommunicate = new DesignCommunicate();
        arrayList.add(designCommunicate);
        communicateList.setTable(arrayList);
        communicateList.setType(designCommunicateList.getType());
        communicateList.setProjectId(designCommunicateList.getProjectId());

        if (CollectionUtil.isNotEmpty(designCommunicates)){
            communicateList.setTable(designCommunicates);
        }
        return communicateList;
    }

    @Override
    @DSTransactional
    public boolean insert(DesignCommunicateList designCommunicateList) {
        List<DesignCommunicate> table = designCommunicateList.getTable();
        if (CollectionUtil.isNotEmpty(table) && !Objects.isNull(designCommunicateList.getProjectId())){
            List<Long> idList = table.stream()
                    .map(DesignCommunicate::getId)
                    .filter(Objects::nonNull)
                    .distinct()
                    .collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(idList)){
                designCommunicateMapper.deleteBatchIds(idList);
            }
            table.forEach(e ->{
                e.setProjectId(designCommunicateList.getProjectId());
                e.setType(designCommunicateList.getType());
                e.setId(null);
            });
            this.saveBatch(table);
        }
        return true;
    }


    @Override
    public boolean update(List<DesignCommunicate> designCommunicateList) {
        return false;
    }
}
